package net.hn.hnms.biz.cockpit.service.impl;

import com.alibaba.fastjson.JSONObject;
import lombok.RequiredArgsConstructor;
import net.hn.hnms.biz.cockpit.domain.vo.*;
import net.hn.hnms.biz.cockpit.mapper.SafeCockpitMapper;
import net.hn.hnms.biz.cockpit.service.ISafeCockpitService;
import net.hn.hnms.biz.cockpit.utils.CalculateUtil;
import net.hn.hnms.sys.common.core.utils.DateUtils;
import net.hn.hnms.sys.common.core.utils.StringUtils;
import org.apache.commons.compress.utils.Lists;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class SafeCockpitServiceImpl implements ISafeCockpitService {

    private final SafeCockpitMapper safeCockpitMapper;

    @Override
    public List<RiskAreaVo> getRiskAreaList(String mineCode) {
        // 查询最新日期的区域风险统计
        return safeCockpitMapper.getRiskAreaList(mineCode);
    }

    @Override
    public List<RiskTypeVo> getRiskTypeList(String mineCode) {
        return safeCockpitMapper.getRiskTypeList(mineCode);
    }

    @Override
    public RiskLevelVo getRiskLevelInfo(String mineCode) {
        RiskLevelVo riskLevelVo = safeCockpitMapper.getRiskLevelInfo(mineCode);
        if (riskLevelVo == null) {
            riskLevelVo = new RiskLevelVo();
        } else {
            riskLevelVo.setRiskTotal(riskLevelVo.getHighRisk()+riskLevelVo.getMajorRisk()+riskLevelVo.getGeneralRisk()+riskLevelVo.getLowRisk());
            riskLevelVo.setGeneralRiskRate(CalculateUtil.getPercent(riskLevelVo.getRiskTotal().toString(), riskLevelVo.getGeneralRisk().toString()))
                    .setLowRiskRate(CalculateUtil.getPercent(riskLevelVo.getRiskTotal().toString(), riskLevelVo.getLowRisk().toString()))
                    .setHighRiskRate(CalculateUtil.getPercent(riskLevelVo.getRiskTotal().toString(), riskLevelVo.getHighRisk().toString()))
                    .setMajorRiskRate(CalculateUtil.getPercent(riskLevelVo.getRiskTotal().toString(), riskLevelVo.getMajorRisk().toString()));
        }
        return riskLevelVo;
    }

    @Override
    public JSONObject getMonthHiddenTrouble(String mineCode) {
        JSONObject result = new JSONObject();
        String curYear = DateUtils.dateTimeNow(DateUtils.YYYY);
        String lastYear = DateUtils.parseDateToStr(DateUtils.YYYY, DateUtils.addYears(DateUtils.getNowDate(), -1));
        result.put("legendData", Arrays.asList(lastYear, curYear));
        result.put("unit", "个");
        List<String> xData = Lists.newArrayList();
        // 获取当前日期
        LocalDate currentDate = LocalDate.now();
        int monthValue = currentDate.getMonthValue();
        for (int i = 1; i <= monthValue; i++) {
            xData.add(StringUtils.leftPad("" + i, 2, '0'));
        }
        // 根据年份获取本年数据
        List<OptionVo> curYearData = safeCockpitMapper.getMonthHiddenTrouble(mineCode, curYear);
        List<OptionVo> lastYearData = safeCockpitMapper.getMonthHiddenTrouble(mineCode, lastYear);
        List<String> curYearList = Lists.newArrayList();
        List<String> lastYearList = Lists.newArrayList();
        for (String x : xData) {
            OptionVo optionVo = curYearData.stream().filter(item -> item.getName().equals(curYear + "-" + x)).findFirst().orElse(null);
            curYearList.add(optionVo == null ? "0" : optionVo.getValue());
            optionVo = lastYearData.stream().filter(item -> item.getName().equals(lastYear + "-" + x)).findFirst().orElse(null);
            lastYearList.add(optionVo == null ? "0" : optionVo.getValue());
        }
        result.put("xData", xData);
        result.put("yData", Arrays.asList(lastYearList, curYearList));
        return result;
    }

    @Override
    public List<RiskHiddenTroubleVo> getRiskHiddenTrouble(String mineCode) {
        return safeCockpitMapper.getRiskHiddenTrouble(mineCode);
    }

    @Override
    public JSONObject getMajorHiddenTrouble(String mineCode) {
        JSONObject result = new JSONObject();
        String curYear = DateUtils.dateTimeNow(DateUtils.YYYY);
        String lastYear = DateUtils.parseDateToStr(DateUtils.YYYY, DateUtils.addYears(DateUtils.getNowDate(), -1));
        result.put("legendData", Arrays.asList(lastYear, curYear));
        result.put("unit", "个");
        List<SafeDangerMajorVo> list = safeCockpitMapper.getMajorHiddenTrouble(mineCode, Arrays.asList(curYear, lastYear));
        Set<String> xData = list.stream().map(SafeDangerMajorVo::getMajorName).collect(Collectors.toSet());
        List<String> curYearList = Lists.newArrayList();
        List<String> lastYearList = Lists.newArrayList();
        xData.forEach(item -> {
            SafeDangerMajorVo curYearVo = list.stream().filter(vo -> vo.getYear().equals(curYear) && vo.getMajorName().equals(item)).findFirst().orElse(null);
            curYearList.add(curYearVo == null ? "0" : curYearVo.getTroubleTotal().toString());
            SafeDangerMajorVo lastYearVo = list.stream().filter(vo -> vo.getYear().equals(lastYear) && vo.getMajorName().equals(item)).findFirst().orElse(null);
            lastYearList.add(lastYearVo == null ? "0" : lastYearVo.getTroubleTotal().toString());
        });
        result.put("xData", xData);
        result.put("yData", Arrays.asList(lastYearList, curYearList));
        return result;
    }


}
